<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            box-sizing: border-box;
        }

        body {
            background: #3c90ff;
        }

        #weatherDiv {
            display: flex;
        }

        .flex {
            margin: 0 20px;
            text-align: center;
            margin-top: 100px;
        }

        .weatherBlock {
            width: 1000px;
            height: 400px;
            display: flex;
            background: #fff;
            flex-direction: column;
            align-items: center;
            margin: 30px auto;
            position: relative;
            border-radius: 20px;
        }

        .weatherBlock h2 {
            position: absolute;
            top: 10px;
            left: 20px;
        }
    </style>
</head>

<body>
    <div class="weatherBlock" id="mydiv">
        <h2>郑州七日内天气预报</h2>
        <div id="weatherDiv">

        </div>

    </div>

    <script text="text/javascript">

        var xhr = new XMLHttpRequest();

        xhr.addEventListener("readystatechange", function () {
            if (this.readyState === 4) {
                const a = JSON.parse(this.responseText)
                console.log(a.data);
                var weas = []
                document.getElementById("weatherDiv").innerHTML = ""
                a.data.forEach(element => {
                    let inrArr = ['烈日', '多云', '阴', '小雨', '大雨', '雪', '雾', '中雨']
                    let target = ""
                    switch (element.wea) {
                        case "多云转晴":
                        case "阴转晴":
                        case "晴":
                            target = inrArr[0]
                            break;
                        case "晴转多云":
                        case "多云":
                            target = inrArr[1]
                            break;
                        case "小雨转阴":
                        case "晴转阴":
                        case "阴":
                            target = inrArr[2]
                            break;
                        case "小雨":
                            target = inrArr[3]
                            break;
                        case "雪":
                            target = inrArr[5]
                            break;
                        case "小雨转中雨":
                        case "中雨":
                            target = inrArr[7]
                            break;
                        case "大雨":
                            target = inrArr[4]
                            break;
                        default:
                            break;
                    }
                    let date = new Date(element.date).toLocaleDateString()
                    let text = ""
                    switch (date) {
                        case new Date().toLocaleDateString():
                            text = "今天"
                            break;
                        case new Date(new Date().getTime() + 24 * 60 * 60 * 1000).toLocaleDateString():
                            text = "明天"
                            break;
                        case new Date(new Date().getTime() + 48 * 60 * 60 * 1000).toLocaleDateString():
                            text = "后天"
                            break;
                        default:
                            text = element.week
                            break;
                    }
                    let b = `<div class = 'flex'>
                                <p>${text}</p>
                                <p>${String(element.date).substring(5, 7) + "月" + String(element.date).substring(8)}日</p>
                                <img src="img/${target}.svg" alt="" width="20px" height="20px">
                                <p>${element.wea}</p>
                                <p>${element.tem1}&nbsp;-&nbsp;${element.tem2}°C</p>
                                <p>${element.win[0]}&nbsp;${element.win_speed}</p>
                            </div>`
                    document.getElementById("weatherDiv").innerHTML += b
                    console.log();
                });
            }
        });

        xhr.open("GET", "http://gfeljm.tianqiapi.com/api?unescape=1&version=v9&appid=17893453&appsecret=lNi9xcCZ");

        xhr.send();
    </script>
</body>

</html>